/**
*管理员管理js
*/
$(function() {
	initAddAdminBtn();

	$.ajax({
				type : "POST",
				url : "loadAdmins",
				dataType : "json",
				success : function(data) {
					var roleId= $("#roleId").val();
					$("#waiting-bar").hide();
					var htmlStr = "";
					if (data.adminList.length == 0) {
						$("#emptyData").show();
					} else {
						for (var i = 0; i < data.adminList.length; i++) {
							htmlStr += "<tr><td>" + (i + 1) + "</td>";
							htmlStr += "<td>" + data.adminList[i].username + "</td>";
							htmlStr += "<td>" + (data.adminList[i].role==1?'超级管理员':'普通管理员' )+ "</td>";
							
							if (data.adminList[i].status == 1) {
								if(roleId==0){
									htmlStr +="<td class='text-success'>启用中</td>";
								}else if(roleId==1&&data.adminList[i].role!=1){
									htmlStr += "<td><button type='button' class='btn btn-default' onclick='forbid("
										+ data.adminList[i].id
										+ ","
										+ data.adminList[i].status
										+ ")'>禁用</button>";
								}else{
									htmlStr +="<td></td>";
								}
							} else {
								if(roleId==0){
									htmlStr +="<td class='text-danger'>禁用中</td>";
								}else if(roleId==1&&data.adminList[i].role!=1){
									htmlStr += "<td><button type='button' class='btn btn-primary' onclick='forbid("
										+ data.adminList[i].id
										+ ","
										+ data.adminList[i].status
										+ ")'>启用</button>";
								}else{
									htmlStr +="<td></td>";
								}
							}
							if(roleId==1&&data.adminList[i].role!=1){
								htmlStr += "<button type='button' class='btn btn-danger' onclick='del("
									+ data.adminList[i].id
									+ ")'>删除</button></td></tr>";
							}
						}
						$("#admin_tb").show();
					}
					$("#admin_tbody").html(htmlStr);
					initDataTable();
				}
			});
})

function initAddAdminBtn() {
	$('#parentIframe')
	.on(
			'click',
			function() {
				layer
						.open({
							type : 1,
							title : '添加管理员',
							area : [ '600px', '360px' ],
							shadeClose : true, //点击遮罩关闭
							content : '<div class="ibox-content form-horizontal">\
							        <div class="form-group">\
							            <label class="col-sm-4 control-label"><span class="text-danger">*</span>登录名：</label>\
							            <div class="col-sm-5">\
							                <input class="form-control" type="text" id="username">\
							            </div>\
							        </div>\
							        <div class="form-group">\
							            <label class="col-sm-4 control-label"><span class="text-danger">*</span>角色：</label>\
							            <div class="col-sm-5">\
							            <select class="form-control" id="role">\
							                <option value="0">普通管理员</option>\
							                <option value="1">超级管理员</option>\
							            </select>\
							            </div>\
							        </div>\
								 	<div class="form-group">\
					            		<div class="col-sm-5 col-sm-offset-4">\
					                	<button class="btn btn-primary layui-layer-close" type="button" onclick="addAdmin();">&#8195;添加&#8195;</button><!--layui-layer-close关闭弹出框-->\
					            		</div>\
									</div>\
								</div>'

						});
			});
}

function initDataTable() {
	$('.dataTables-example').dataTable({
		"oLanguage" : { // 汉化
			"sProcessing" : "正在加载数据...",
			"sLengthMenu" : "_MENU_记录/页 ",
			"sZeroRecords" : "没有您要搜索的内容",
			"sInfo" : "_START_ 至 _END_  ，共 _TOTAL_ 项",
			"sInfoEmpty" : "记录数为0",
			"sInfoFiltered" : "(全部记录数 _MAX_  条)",
			"sInfoPostFix" : "",
			"sSearch" : "查找&nbsp;&nbsp;",
			"sUrl" : "",
			"oPaginate" : {
				"sFirst" : "第一页",
				"sPrevious" : " 上一页 ",
				"sNext" : " 下一页 ",
				"sLast" : " 最后一页 "
			}
		}
	});
}

function forbid(id, status) {
	var url = "forbidAdmin";
	var operateDesc = "禁用";
	if (status == 0) {
		url = "unforbidAdmin";
		operateDesc = "启用";
	}
	$.ajax({
		type : "POST",
		url : url,
		data : {
			"id" : id
		},
		dataType : "json",
		success : function(data) {
			if (data) {
				swal({
					title : operateDesc + "成功！",
					type : "success",
					showCancelButton : false,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "OK",
					closeOnConfirm : false
				}, function() {
					window.location.reload();
				});
			}
		}
	})
}

function del(id) {
	swal({
		title : "您确定要删除这条信息吗",
		text : "删除后将无法恢复，请谨慎操作！",
		type : "warning",
		showCancelButton : true,
		confirmButtonColor : "#DD6B55",
		confirmButtonText : "删除",
		cancelButtonText : "取消",
		closeOnConfirm : false
	}, function() {
		$.ajax({
			type : "POST",
			url : "delAdmin",
			data : {
				"id" : id
			},
			dataType : "json",
			success : function(data) {
				if (data) {
					swal({
						title : "删除成功!",
						type : "success",
						showCancelButton : false,
						confirmButtonColor : "#DD6B55",
						confirmButtonText : "OK",
						closeOnConfirm : false
					}, function() {
						window.location.reload();
					});
				}
			}
		})
	});
}

function addAdmin() {
	var username = $("#username").val();
	var role = $("#role").val();
	if (username == "" || username == null) {
		swal("管理员名称不能为空！", "请填写管理员名称！", "info");
		return;
	}
	if(username.length<6){
		swal("管理员账号太短！", "管理员账号长度至少为6位", "info");
		return;
	}
	$.ajax({
		type : "POST",
		url : "addAdmin",
		data : {
			"username" : username,
			"role" : role
		},
		dataType : "json",
		success : function(data) {
			if (data.msg.stateDescStr == "SUCCESS") {
				swal({
					title : data.msg.message,
					type : "success",
					showCancelButton : false,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "OK",
					closeOnConfirm : false
				}, function() {
					window.location.reload();
				});
			} else {
				swal("添加失败！", data.msg.message, "warning");
			}
		}
	})
}